﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ANGFLib;

namespace ANGFSessionWrapper
{
    public static class Util
    {
        public static DateTime JounaligStartDateTime { get; set; }
        public static void MainLoop()
        {
            //State.WarpTo(new PlaceFrontMenu());
            for (; ; )
            {
                bool result = ScheduleCheck.EventCheck(Flags.Now, Flags.Now);
                if (result) continue;

                ScheduleCheck.SuppokashiCheckAndDo();

                foreach (ANGFLib.Module n in State.loadedModules)
                {
                    n.OnBeforeCommand();
                }

                if (Flags.Now != DateTime.MinValue && State.今日の就寝時刻 != DateTime.MinValue && Flags.Now >= State.今日の就寝時刻)
                {
                    DefaultPersons.システム.Say("既に今日の就寝時刻を過ぎています。すぐに帰宅、就寝します。");
                    State.GoNextDayMorning();
                    continue;
                }

                List<SimpleMenuItem> list = new List<SimpleMenuItem>();
                var list2 = General.FindPersonsWithPlace(State.CurrentPlace.Id);
                if (list2.Length > 0)
                {
                    list.Add(new SimpleMenuItem("話す", General.Talk));
                }
                bool goodflag = State.CurrentPlace.ConstructMenu(list);
                foreach (ANGFLib.Module n in State.loadedModules)
                {
                    if (!goodflag) break;
                    goodflag &= n.ConstructMenu(list, State.CurrentPlace);
                }
                if (!goodflag)
                {
                    State.CurrentPlace.OnMenu();
                }
                else
                {
                    if (General.CanMove(General.candMoveMenu, Moves.HowToMove))
                    {
                        list.Add(new SimpleMenuItem("移動", () => General.MoveMenu(General.candMoveMenu, Moves.HowToMove)));
                    }
                    if (General.CanMove(General.candSubMoveMenu, Moves.HowToSubMove))
                    {
                        list.Add(new SimpleMenuItem("サブ移動", () => General.MoveMenu(General.candSubMoveMenu, Moves.HowToSubMove)));
                    }

                    if ((State.MenuStopMaps & MenuStopControls.System) == 0)
                    {
                        UI.SimpleMenu("どうしようか", list.ToArray());
                    }
                    else
                    {
                        UI.SimpleMenuWithoutSystem("どうしようか", list.ToArray());
                    }
                }
                if (State.CurrentPlace.Id == "") break;
            }
        }
    }
}